package xml.xstream;


import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import lombok.SneakyThrows;
import org.junit.Test;
import xml.xstream.pojo.Beans;
import xml.xstream.pojo.Dlineage;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Author longhj
 * @Description TODO
 * @Date 2021/08/06 星期五 23:48
 */
public class XStreamMain {
    @SneakyThrows
    public static void main(String[] args) {

        String ss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<beans>\n" +
                "\t<bean id=\"exportTrigger\" class=\"org.springframework.scheduling.quartz.SimpleTriggerFactoryBean\">\n" +
                "\t\t<property name=\"jobDetail\">\n" +
                "\t\t\t<bean class=\"org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean\">\n" +
                "\t\t\t\t<property name=\"targetObject\" ref=\"exportService\"></property>\n" +
                "\t\t\t\t<property name=\"targetMethod\" value=\"doExport\"></property>\n" +
                "\t\t\t</bean>\n" +
                "\t\t</property>\n" +
                "\t</bean>\n" +
                "\t<bean id=\"scheduler\" class=\"org.springframework.scheduling.quartz.SchedulerFactoryBean\"/>\n" +
                "\t<bean class=\"org.springframework.scheduling.quartz.SchedulerAccessorBean\">\n" +
                "\t\t<property name=\"triggers\">\n" +
                "\t\t\t<list>\n" +
                "\t\t\t\t<ref bean=\"exportTrigger\"/>\n" +
                "\t\t\t\t<ref bean=\"importTrigger\"/>\n" +
                "\t\t\t</list>\n" +
                "\t\t</property>\n" +
                "\t</bean>\n" +
                "\t<bean id=\"importTrigger\" class=\"org.springframework.scheduling.quartz.SimpleTriggerFactoryBean\">\n" +
                "\t\t<property name=\"jobDetail\">\n" +
                "\t\t\t<bean class=\"org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean\">\n" +
                "\t\t\t\t<property name=\"targetObject\" ref=\"importService\"/>\n" +
                "\t\t\t\t<property name=\"targetMethod\" value=\"doImport\"/>\n" +
                "\t\t\t</bean>\n" +
                "\t\t</property>\n" +
                "\t\t<property name=\"repeatInterval\" value=\"1000\"></property>\n" +
                "\t\t<property name=\"repeatCount\" value=\"1\"></property>\n" +
                "\t</bean>\n" +
                "\t<bean id=\"exportService\" class=\"org.springframework.scheduling.quartz.QuartzTestBean\"/>\n" +
                "\t<bean id=\"importService\" class=\"org.springframework.scheduling.quartz.QuartzTestBean\"/>\n" +
                "</beans>\n";
        InputStream in = XStreamMain.class.getClassLoader().getResourceAsStream("test_xml.xml");
        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
         ss = sb.toString();
        XStream xstream = new XStream(new StaxDriver());
        xstream.processAnnotations(Beans.class);
        xstream.autodetectAnnotations(true);
        //去除执行的结束的安全警告
        xstream.allowTypesByRegExp(new String[]{".*"});
        Beans o = (Beans) xstream.fromXML(ss);
        System.out.println(o);
        String s = xstream.toXML(o);
        System.out.println(s);


    }

    @Test
    public void test() {
        String xml = "<dlineage>\n" +
                "   <relation id=\"3\" type=\"dataflow\">\n" +
                "      <target coordinate=\"[1,27],[1,31]\" column=\"NAME\" id=\"5\" parent_id=\"1\" parent_name=\"TABLEA\"/>\n" +
                "      <source coordinate=\"[1,27],[1,31]\" column=\"NAME\" id=\"1\" parent_id=\"2\" parent_name=\"B\"/>\n" +
                "   </relation>\n" +
                "   <relation id=\"4\" type=\"dataflow\">\n" +
                "      <target coordinate=\"[1,32],[1,35]\" column=\"AGE\" id=\"6\" parent_id=\"1\" parent_name=\"TABLEA\"/>\n" +
                "      <source coordinate=\"[1,32],[1,35]\" column=\"AGE\" id=\"2\" parent_id=\"2\" parent_name=\"B\"/>\n" +
                "   </relation>\n" +
                "   <table name=\"TABLEA\" id=\"1\" type=\"table\" coordinate=\"[1,13],[1,19]\">\n" +
                "      <column name=\"NAME\" id=\"5\" coordinate=\"[1,27],[1,31]\"/>\n" +
                "      <column name=\"AGE\" id=\"6\" coordinate=\"[1,32],[1,35]\"/>\n" +
                "   </table>\n" +
                "   <table name=\"B\" id=\"2\" type=\"table\" coordinate=\"[1,42],[1,43]\">\n" +
                "      <column name=\"NAME\" id=\"1\" coordinate=\"[1,27],[1,31]\"/>\n" +
                "      <column name=\"AGE\" id=\"2\" coordinate=\"[1,32],[1,35]\"/>\n" +
                "   </table>\n" +
                "</dlineage>";
        XStream xStream = new XStream();
        xStream.autodetectAnnotations(true);
        xStream.processAnnotations(Dlineage.class);
        Dlineage carInfos = (Dlineage) xStream.fromXML(xml);
        System.out.println();
    }
}
